Skip to content

feat(code): execute inline !bash commands in cloud sandbox#1707

Open
VojtechBartos wants to merge 2 commits intomainfrom
posthog-code/sandboxed-bash
Open

feat(code): execute inline !bash commands in cloud sandbox#1707
VojtechBartos wants to merge 2 commits intomainfrom
posthog-code/sandboxed-bash

Conversation

@VojtechBartos
Copy link
Copy Markdown
Member

Problem

Inline !command bash was disabled for cloud runs — the handler used the local Shell service (node-pty) which can't reach the sandbox container.

Solution

Added a shell_execute JSON-RPC method on the agent-server that spawns bash -c <command> inside the sandbox and streams stdout/stderr as SSE notifications. Renderer pre-generates an executionId, sends the command via CloudTaskService.sendCommand, and resolves when shell_exit arrives. Requires a paired backend PR to allowlist the new method.

Prerequisites

PostHog/posthog#55064

Showcase

code-bash-cloud.mov

Copy link
Copy Markdown
Contributor

@tatoalo tatoalo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments, thinking largely at how this could be abused 👀

Comment on lines +2599 to +2603
if (pending) {
if (update.stream === "stdout") {
pending.stdout += update.chunk;
} else {
pending.stderr += update.chunk;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we bound this?

Comment on lines +2051 to +2055
const child = spawn("bash", ["-c", command], {
cwd,
env: process.env,
stdio: ["ignore", "pipe", "pipe"],
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't we leaking envs here possibly?

pending.resolve({
stdout: pending.stdout,
stderr: pending.stderr,
exitCode: update.exitCode ?? (update.signal ? 1 : 0),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit for consistency, signal should not conflict with exit codes

- Don't pass process.env to shell spawns — only PATH and HOME, preventing
  API keys and tokens from leaking to user commands
- Cap concurrent shell executions to 5 on both server and client side
- Use Unix 128+N convention for signal exit codes instead of hardcoded 1
- Gate cloud bash to runs with a sandbox environment attached — threads
  sandboxEnvironmentId through session creation and checks it before
  allowing !command execution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants